<template>
<el-row>
  <el-col :span="12">
  <img src="../../static/images/login-img.png" alt="" class="fr loginImg">
   </el-col>
   <el-col :span="12">
    <el-form  class="login-container fl" label-position="left"
           label-width="0px" v-loading="loading">
    <h3 class="login_title">业务管理系统{{testTitle}}登录</h3>
    <div class="select" style="height:50px;">
      <i class="el-icon-goods"  @click="selectMethod('password')" :class="{sel:loginType=='password'}">账号密码登录</i>
      <i class="el-icon-mobile-phone" @click="selectMethod('code')" :class="{sel:loginType=='code'}">手机号验证码登录</i>
    </div>
    <div v-show="loginType=='password'">
        <el-form-item prop="account">
          <el-input type="text" v-model="loginForm.username" @keyup.enter.native="submitClick"
                    auto-complete="off" placeholder="账号" clearable></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <el-input type="password" v-model="loginForm.password" @keyup.enter.native="submitClick"
                    auto-complete="off" placeholder="密码" clearable></el-input>
        </el-form-item>
        <!-- <el-checkbox class="login_remember" v-model="checked"
                    label-position="left">记住密码
        </el-checkbox>   -->
    </div>
    <div class="code" v-show="loginType=='code'">
      <el-form-item prop="account">
          <el-input type="text" v-model="loginForm.username" @keyup.enter.native="submitClick"
                    auto-complete="off" placeholder="手机号" clearable></el-input>
      </el-form-item>
      <el-form-item prop="checkCode">
          <el-input type="text" v-model="loginForm.code" @keyup.enter.native="submitClick"
                    auto-complete="off" placeholder="验证码" clearable class="codeinput">
            
          </el-input>
          <el-button type="primary" round class="getCode" @click="getCode()" :disabled="btnState.dis">{{btnState.text}}</el-button>
      </el-form-item>
    </div> 
  
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%" @click="submitClick">登录</el-button>
    </el-form-item>
  </el-form>
 </el-col>
  </el-row>
</template>
<script>
export default {
  data() {
    return {
      testTitle:"",
      loginType: "password",
      // rules: {
      //   account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
      //   checkPass: [{ required: true, message: "请输入密码", trigger: "blur" }],
      //   checkCode: [
      //     { required: true, message: "请输入验证码", trigger: "blur" }
      //   ]
      // },
      checked: false,
      loginForm: {
        username: "",
        password: "",
        code: "", 
      },
      btnState:{
        text:"获取验证码",
        dis:false,
      },
      loading: false
    };
  },
  created() {
    // var that = this;
    // document.onkeydown = function(e) {
    //   console.log(e)
    //   var key = window.event.keyCode;
    //   if (key == 13) {
    //     that.submitClick()
    //   }
    // }
    if(location.port=='8086'){
      this.testTitle="（测试版）"
    }
  },
  methods: {
    selectMethod: function(method) {
      this.loginForm.username = "";
      this.loginForm.password = "";
      this.loginForm.code = "";
      this.loginType = method;
    },
    getCode:function(){
      if(this.common.isNull(this.loginForm.username)){
        this.$message({
          message: '请输入手机号',
          type: 'warning'
        });
        return false;
      }
      this.getRequest("/common/sendPhoneCaptcha?phone="+this.loginForm.username).then(resp => { 
          if(resp && resp.data.status==200){
            this.$message({
              type: "success",
              message: resp.data.msg
            });
            this.btnState.dis=true
            this.codeTime()
          }
          
        });

    },
    submitClick: function() {
      var _this = this;
      // 账号密码登录
      if (this.loginType == "password") {
        if(this.common.isNull(this.loginForm.username) || this.common.isNull(this.loginForm.password)){
          this.$message({
            message: '请输入账号和密码',
            type: 'warning'
          });
          return false
        }
        this.loading = true;
        this.postRequest("/login", {
          username: this.loginForm.username,
          password: this.loginForm.password
        }).then(resp => {
          _this.loading = false;  
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.$store.commit("login", data.obj);
            var path = _this.$route.query.redirect;
            _this.$router.replace({
              path: path == "/" || path == undefined ? "/home" : path
            });
          }
        });
      }
      // 手机验证码登录
      else if(this.loginType == "code"){
        if(this.common.isNull(this.loginForm.username) || this.common.isNull(this.loginForm.code)){
          this.$message({
            message: '请输入手机号和验证码',
            type: 'warning'
          });
          return false
        }
        this.loading = true;
        this.postRequest("/login", {
          username: this.loginForm.username,
          password: this.loginForm.code
        }).then(resp => {
          _this.loading = false;
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.$store.commit("login", data.obj);
            var path = _this.$route.query.redirect;
            _this.$router.replace({
              path: path == "/" || path == undefined ? "/home" : path
            });
          }
        });
      }
    },
    codeTime:function(){
      var time=30;
      var that=this;
      var timer=setInterval(function(){
        time--;
        that.btnState.text=time
        if(time==0){
          clearInterval(timer)
          that.btnState.text='获取验证码';
          that.btnState.dis=false;
        }
      },1000)
    }
  }
};
</script>
<style scoped>
.el-row {
  width:100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  border: none;
  background-color: #93defe;
}
h3{
  font-size: 24px;
  font-weight: bold;
}
.loginImg {
  width: 500px;
  height: 440px;
  margin: 100px 50px;
}
.codeinput {
  width: 200px;
  text-align: left;
  float: left;
}
.getCode {
  width: 120px;
  float: right;
}
.select i {
  width: 50%;
  height: 80px;
  line-height: 80px;
  display: block;
  text-align: center;
  float: left;
  cursor: pointer;
  color: #ccc;
}
.select i.sel {
  color: #409eff;
  font-weight: bold;
}
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 120px 200px 0 0;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.login_title {
  margin: 0px auto;
  text-align: center;
  color: #505458;
}
.login_remember {
  margin: 0px 0px 35px 0px;
  text-align: left;
}
</style>
